#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

import json

from generaldb.gbase8a import LOGGER
from generaldb.gbase8a.common.const import JsonConstant
from generaldb.gbase8a.job_manager import JobManager
from generaldb.gbase8a.param.parse_params import BaseParam


class GbaseParseBackupParamParam(BaseParam):
    def __init__(self, json_params, job_manager: JobManager):
        super(GbaseParseBackupParamParam, self).__init__(json_params, job_manager, LOGGER)

    @staticmethod
    def get_base_copy_id(copy_info: dict):
        """
        从备份时上报的副本信息中获取基备份副本id
        """
        base_copy_id = copy_info.get(JsonConstant.EXTEND_INFO, {}).get(JsonConstant.BASE_COPY_ID)
        return base_copy_id

    def get_job_repositories(self):
        repositories = self._json_param.get(JsonConstant.JOB, {}).get(JsonConstant.REPOSITORIES, [])
        return repositories

    def get_backup_type(self):
        """
        获取备份类型：全量/增量/
        """
        return self._json_param.get(JsonConstant.JOB, {}). \
            get(JsonConstant.JOB_PARAM, {}).get(JsonConstant.BACKUP_TYPE)

    def get_resource_type(self):
        """
        获取备份资源类型：实例/数据库
        """
        return self._json_param.get(JsonConstant.JOB, {}). \
            get(JsonConstant.PROTECT_ENV, {}).get(JsonConstant.EXTEND_INFO, {}).get(
            JsonConstant.FIRSTCLASSIFICATION)

    def get_backup_db_name(self):
        """
        获取待备份的数据库名称
        """
        db_name = self._json_param.get(JsonConstant.JOB, {}). \
            get(JsonConstant.PROTECT_OBJECT, {}).get(JsonConstant.NAME)
        return db_name.lower() if db_name else ''

    def get_nodes_info(self):
        """
        获取所有节点信息
        :return:
        """
        return self._json_param.get(JsonConstant.JOB, {}). \
            get(JsonConstant.PROTECT_ENV, {}).get(JsonConstant.NODES)

    def get_protectenv_extendinfo_cluster_info(self):
        """
        获取portectEnv下extendInfo下的cluster_info信息
        """
        cluster_info = self._json_param.get(JsonConstant.JOB, {}). \
            get(JsonConstant.PROTECT_ENV, {}).get(JsonConstant.EXTEND_INFO, {}).get(
            JsonConstant.CLUSTER_INFO)
        cluster_info = json.loads(cluster_info)
        return cluster_info

    def get_vc_name(self):
        """
        获取vc_name
        """
        return self.get_specified_self_define_params(JsonConstant.VC_NAME)

    def get_copys(self):
        """
        获取所有的副本信息
        """
        return self._json_param.get(JsonConstant.JOB, {}). \
            get(JsonConstant.COPY, {})
